package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional  //开启事务
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品（多态）
        save(dishDto);

        //保存口味
        //口味中不含id，获取菜品id
        Long id = dishDto.getId();
        //获取口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        //
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(id);
        }
        //保存菜品口味到菜品口味表中dish_flavors
        dishFlavorService.saveBatch(flavors);
    }

    /**
     * 根据id查询菜品信息和对应的口味信息
     *
     * @param id
     */
    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //根据id获取基本信息
        Dish dish = getById(id);

        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);

        //查询当前菜品对应的口味信息，从dish_flavor表查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);

        return dishDto;
    }

    /**
     * 更新菜品信息，同时更新对应的口味信息
     *
     * @param dishDto
     */
    @Override
    public void updateWithFlavor(DishDto dishDto) {

        //更新菜品信息
        updateById(dishDto);

        //删除当前菜品口味
        LambdaUpdateWrapper<DishFlavor> lqw = new LambdaUpdateWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);

        //添加当前菜品口味
        List<DishFlavor> flavors = dishDto.getFlavors();
        for (DishFlavor flavor : flavors) {
            flavor.setDishId(dishDto.getId());
        }

    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    public void deleteAll(List<Long> ids) {
        //删除列表中含有启售产品，不能删除
        for (Long id : ids) {
            if (getById(id).getStatus() == 1) {
                throw new CustomException("删除列表中含有启售产品，不能删除");
            }
        }
        //删除口味
        LambdaQueryWrapper<DishFlavor> dishlqw = new LambdaQueryWrapper<>();
        dishlqw.in(DishFlavor::getDishId, ids);
        dishFlavorService.remove(dishlqw);
        //删除菜品
        removeByIds(ids);
    }
}
